<template>
  <div>
    <h1 style="font-size: 28px">新增网关</h1>
    <div>
      <a-form :label-col="{ span: 4 }" :wrapper-col="{ span: 14 }">
        <a-form-item label="站点ID" required>
          <a-input v-model:value="site_id"/>
        </a-form-item>
        <a-form-item label="MAC地址" required>
          <a-input v-model:value="mac_address"/>
        </a-form-item>
        <a-form-item label="IMEI">
          <a-input v-model:value="imei"/>
        </a-form-item>
        <a-form-item label="CCID">
          <a-input v-model:value="ccid"/>
        </a-form-item>
        <a-form-item label="CSQ">
          <a-input v-model:value="csq"/>
        </a-form-item>
        <a-form-item label="固件版本">
          <a-input v-model:value="firmware_version"/>
        </a-form-item>
        <a-form-item label="硬件版本">
          <a-input v-model:value="hardware_version"/>
        </a-form-item>
        <a-form-item label="描述">
          <a-textarea v-model:value="intro"/>
        </a-form-item>
        <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" style="width: 140px;" @click="onConfirm">确定</a-button>
          <a-button style="width: 140px;margin-left: 20px;" @click="onBack">返回</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts">

import {defineComponent, reactive, toRefs} from "vue";
import {createGateway} from "/@/api/product/gateway";
import {HttpResult} from "/@/api/core/model/coreModel";
import {message} from "ant-design-vue";

export default defineComponent({
  name: 'GatewayAdd',
  props: {
    viewType: {
      type: String,
      default: null,
    }
  },
  setup(props, {emit}) {
    const data = reactive({
      site_id: "",
      mac_address: "",
      imei: "",
      ccid: "",
      csq: "",
      firmware_version: "",
      hardware_version: "",
      intro: "",
    });

    const onConfirm = () => {
      if (data.site_id.length == 0) {
        message.warn("请输入有效的站点ID");
        return;
      }
      if (data.mac_address.length != 12) {
        message.warn("请输入有效的MAC地址");
        return;
      }
      createGateway({
        site_id: data.site_id,
        mac_address: data.mac_address,
        ccid: data.ccid,
        csq: data.csq,
        imei: data.imei,
        firmware_version: data.firmware_version,
        hardware_version: data.hardware_version,
        intro: data.intro
      }).then((result: HttpResult) => {
        if (result.code == 0) {
          message.success("新增成功");
          emit("my-emit", 'Confirm');
        } else {
          message.error(result.message);
        }
      });
    }

    const onBack = () => {
      emit("my-emit", 'Cancel');
    }

    return {
      ...toRefs(data),
      onConfirm,
      onBack
    };
  }
});

</script>

<style scoped>

</style>
